﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.DataModels;
using ReportDesignerServer.Dto;
using ReportDesignerServer.IRepository;
using ReportDesignerServer.Repository;
using SqlSugar;

namespace ReportDesignerServer.Controllers
{

    /// <summary>
    /// 患者在科操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PatientInHospitalController : ControllerBase
    {
        IPatientInHospitalRepository patientInHospitalRepository;

        public PatientInHospitalController(IPatientInHospitalRepository patientInHospitalRepository)
        {
            this.patientInHospitalRepository = patientInHospitalRepository;
        }


        /// <summary>
        /// 查询到当天没有出院的人
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public Rt QueryPatientInHospital()
        {
            Expressionable<PatientInHospital> exp = Expressionable.Create<PatientInHospital>();
            exp.And(it => it.ISOUT == 0);
            var list = patientInHospitalRepository.GetListByWhere(exp);
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].SID = list[i].ID.ToString();
                }
            }
            return Rt.SuccessObj(list);
        }


        /// <summary>
        /// 通过id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Rt GetPatientInHospitalById(string id)
        {
            long lid = 0;
            if (long.TryParse(id, out lid))
            {
                Expressionable<PatientInHospital> exp = Expressionable.Create<PatientInHospital>();
                exp.And(it => it.ID == lid);
                var model = patientInHospitalRepository.GetListByWhere(exp).FirstOrDefault();
                model.SID = model.ID.ToString();
                return Rt.SuccessObj(model);
            }
            else
            {
                return Rt.Error();
            }
        }


        /// <summary>
        /// 通过月份组查询数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SavePatientInHospital([FromBody] PatientInHospital model)
        {
            long res = 0;
            if (model != null)
            {
                long lid = 0;
                if (long.TryParse(model.SID, out lid))
                {
                    model.ID = lid;
                }
                if (model.ID == 0)
                {
                    Expressionable<PatientInHospital> exp = Expressionable.Create<PatientInHospital>();
                    exp.And(it => it.PATIENTID == model.PATIENTID);
                    exp.And(it => it.ISOUT == 0);
                    var patList = patientInHospitalRepository.GetListByWhere(exp);
                    if (patList?.Count == 0)
                    {
                        //所有添加的数据，都是未出科
                        model.ISOUT = 0;
                        res = patientInHospitalRepository.ExecuteReturnSnowflakeId(model);
                    }
                    else { 
                    
                        return Rt.Fail("该患者ID已存在未出科数据！");
                    }
                }
                else
                {
                    res = patientInHospitalRepository.Update(model);
                }
            }
            if (res > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }
    }
}
